package com.qf.service.impl;

import com.qf.dao.CartDao;
import com.qf.dao.OrderDao;
import com.qf.dao.OrderDetailDao;
import com.qf.dao.impl.CartDaoImpl;
import com.qf.dao.impl.OrderDaoImpl;
import com.qf.dao.impl.OrderDetailDaoImpl;
import com.qf.entity.Address;
import com.qf.entity.Cart;
import com.qf.entity.Order;
import com.qf.entity.OrderDetail;
import com.qf.service.AddressService;
import com.qf.service.OrderService;
import com.qf.utils.DataSourceUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package com.qf.service.impl
 * @description: ${TODO}
 * @date 2022/6/24 16:44
 */
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao=new OrderDaoImpl();
    private OrderDetailDao orderDetailDao=new OrderDetailDaoImpl();
    private CartDao cartDao=new CartDaoImpl();
    @Override
    public void submitOrder(Order order, List<Cart> cartList) {
        //开启事务  begin; start transaction ; set autocommit=0
        try {
            DataSourceUtils.begin();
            //设计到三个表
            //1 向tb_order表添加一条记录
            orderDao.insert(order);
            //int i=10/0;
            //2 向tb_detail表添加订单详情
            if (cartList != null) {
                for (Cart cart : cartList) {
                    OrderDetail orderDetail=new OrderDetail(0, order.getId(), cart.getPid(), cart.getNum(),cart.getMoney());
                    orderDetailDao.insert(orderDetail);
                }
            }
            //3 删除购物车中信息
            cartDao.deleteByUid(order.getUid());
            //成功提交事务
            DataSourceUtils.commit();
        }catch (Exception e){
            //失败回滚事务
            try {
                DataSourceUtils.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            throw new RuntimeException(e);
        }finally {
            //释放资源
            try {
                DataSourceUtils.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void updateStatus(String oid, String status) {
        orderDao.updateStatus(oid,status);
    }

    @Override
    public List<Order> findByUid(int uid) {
        List<Order> orderList = orderDao.selectByUid(uid);
        //查询地址
        AddressService addressService=new AddressServiceImpl();
        List<Address> addList = addressService.findByUid(uid);
        if (orderList != null) {
            for (Order order : orderList) {
                //获取地址编号
                Integer aid = order.getAid();
                //遍历地址集合
                for (Address address : addList) {
                    if(aid==address.getId()){//找到地址了
                        order.setAddress(address);
                    }
                }
            }
        }
        return orderList;
    }

    @Override
    public Order findByOid(String oid) {
        Order order=orderDao.selectByOid(oid);
        if (order != null) {
            AddressService addressService=new AddressServiceImpl();
            Address address=addressService.findById(order.getAid());
            order.setAddress(address);
        }
        return order;
    }

    @Override
    public List<Order> findAll() {
        return orderDao.selectAll();
    }

    @Override
    public List<Order> findByWhere(String where, List<Object> params) {
        return orderDao.selectByWhere(where,params);
    }
}
